package com.losl.common.alioss;

import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ListObjectsRequest;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@ConfigurationProperties(prefix = "aliyun")
@Data
public class AliOssService {
    private String accessKeyId;

    private String accessKeySecret;

    private String endpoint;

    private String bucketName;

    private String urlPrefix;

    public Boolean putFile(File file, String fileKey) {
        OSS client = null;
        try {
            client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            client.putObject(bucketName, fileKey, file);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            client.shutdown();
        }
        return true;
    }

    public Boolean putFile(InputStream is, String fileKey) {
        OSS client = null;
        try {
            client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            client.putObject(bucketName, fileKey, is);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            client.shutdown();
        }
        return true;
    }

    public Boolean deleteObject(String fileKey) {
        OSS ossClient = null;
        try {
            ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            ossClient.deleteObject(bucketName, fileKey);
            return true;
        } catch (Exception ce) {
            ce.printStackTrace();
        } finally {
            ossClient.shutdown();
        }
        return false;
    }

    public List<Object> listObjects(String prefix, Integer maxKeys) {
        OSS ossClient = null;
        List<Object> list = new ArrayList<>();
        try {
            ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            ListObjectsRequest lor = new ListObjectsRequest(bucketName);
            if (StrUtil.isNotBlank(prefix)) {
                lor.withPrefix(prefix);
            }
            if (null != maxKeys && maxKeys > 0) {
                lor.setMaxKeys(maxKeys);
            }
            ObjectListing objectListing = ossClient.listObjects(lor);
            for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                Map<String, String> map = new HashMap<>();
                map.put("url", urlPrefix + objectSummary.getKey());
                list.add(map);
            }
        } catch (Exception ce) {
            ce.printStackTrace();
        } finally {
            ossClient.shutdown();
        }
        return list;
    }
}
